package com.doyens.cart.service;

import com.doyens.auth.entity.UserInfo;
import com.doyens.cart.client.GoodsClient;
import com.doyens.cart.interceptor.LoginInterceptor;
import com.doyens.cart.pojo.Cart;
import com.doyens.common.utils.JsonUtils;
import com.doyens.item.pojo.Sku;
import io.netty.util.concurrent.NonStickyEventExecutorGroup;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private GoodsClient goodsClient;

    private static final String KEY_PREFIX = "user:cart:";

    public void addCart(Cart cart) {
        //获取获取信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();

        //获取hash
        BoundHashOperations<String, Object, Object> hashOperations = this.redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());
        //查询购物车记录
        String key = cart.getSkuId().toString();
        //购物数量
        Integer num = cart.getNum();
        //判断当前的商品是否在购物车中
       if(hashOperations.hasKey(key)){
           //在,更新数量
           String cartJson = hashOperations.get(key).toString();
           cart = JsonUtils.parse(cartJson, Cart.class);
           cart.setNum(cart.getNum() + num);
           //再存到redis中
         //  hashOperations.put(key,JsonUtils.serialize(cart));
       }else{
           //不在,进行添加
           //cart.set
           Sku sku = this.goodsClient.queryBySkuId(cart.getSkuId());
           cart.setUserId(userInfo.getId());
           cart.setImage(StringUtils.isBlank(sku.getImages()) ? "" : StringUtils.split(sku.getImages(),",")[0]);
           cart.setPrice(sku.getPrice());
           cart.setTitle(sku.getTitle());
           cart.setOwnSpec(sku.getOwnSpec());
       }
       //存入redis中
        hashOperations.put(key,JsonUtils.serialize(cart));




    }

    //查询登录后的购物车
    public List<Cart> queryCarts() {
        //获取获取信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();

        if (!this.redisTemplate.hasKey(KEY_PREFIX + userInfo.getId())){//如果没有取到数据
            return null;
        }


        //获取hash
        BoundHashOperations<String, Object, Object> hashOperations = this.redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());

        List<Object> cartsJson = hashOperations.values();

        if (CollectionUtils.isEmpty(cartsJson)){
            return null;
        }

        return cartsJson.stream().map(cartsJsons -> JsonUtils.parse(cartsJsons.toString(),Cart.class)).collect(Collectors.toList());

    }

    //修改数量
    public void updateNum(Cart cart) {
        //获取获取信息
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        if (!this.redisTemplate.hasKey(KEY_PREFIX + userInfo.getId())){//如果没有取到数据
            return ;
        }
        //获取hash
        BoundHashOperations<String, Object, Object> hashOperations = this.redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getId());

        Integer num = cart.getNum();
        String cartJson = hashOperations.get(cart.getSkuId().toString()).toString();
        cart = JsonUtils.parse(cartJson,Cart.class);
        cart.setNum(num);
        hashOperations.put(cart.getSkuId().toString(),JsonUtils.serialize(cart));
    }
}
